package com.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;

import com.bean.ProductFeature;
import com.mapper.ProductFeatureMapper;

public class Test3 {
	private SqlSessionFactory sqlSessionFactory=null;
	private SqlSession sqlSession= null;
	Logger logger= Logger.getLogger(Test2.class);
	private void creatSqlSessionFactory(){
		InputStream inputStream=null;
		try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	public void selectProductFeatureById(Integer id){
		try {
			sqlSession=sqlSessionFactory.openSession();
			ProductFeatureMapper mapper=sqlSession.getMapper(ProductFeatureMapper.class);
			logger.info(mapper.selectProductFeatureById(id));
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
	}
	public void findProductFeature(String strWhere){
		try {
			sqlSession=sqlSessionFactory.openSession();
			ProductFeatureMapper mapper=sqlSession.getMapper(ProductFeatureMapper.class);
			List<ProductFeature>productFeatures=mapper.findProductFeature(strWhere);
			for (ProductFeature productFeature : productFeatures) {
				logger.info(productFeature);
			}
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
		
	}
	public void count(String strWhere){
		try {
			sqlSession=sqlSessionFactory.openSession();
			ProductFeatureMapper mapper=sqlSession.getMapper(ProductFeatureMapper.class);
			logger.info(mapper.count(strWhere));
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
		
	}
	public void insertProductFeature(ProductFeature productFeature){
		try {
			sqlSession=sqlSessionFactory.openSession();
			ProductFeatureMapper mapper= sqlSession.getMapper(ProductFeatureMapper.class);
			int i=mapper.insertProductFeature(productFeature);
			logger.info(i>0?true:false);
			System.out.println(9/0);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
	}
	public static void main(String[] args) {
		Test3 test3= new Test3();
		test3.creatSqlSessionFactory();
		//test3.selectProductFeatureById(1);
		//test3.findProductFeature("1=1");
		//test3.count("1=1");
		ProductFeature productFeature= new ProductFeature();
		productFeature.setProductFeatureTypeId(2);
		productFeature.setProductFeatureDesc("象拔蚌");
		productFeature.setSort(1);
		test3.insertProductFeature(productFeature);
	}
}
